<?php
class DBDriver {
	
	public $connection	= null;
	public $result		= false;

	public function __construct($dbHost, $dbUser, $dbPass, $dbName) {
		$this->connection = mysql_pconnect($dbHost, $dbUser, $dbPass);
		mysql_select_db($dbName, $this->connection);
		mysql_query('SET character_set_results = "utf8"', $this->connection);
	}

	public function dbQuery($sql) {
		if (is_resource($this->connection)) {
			$this->result = mysql_query($sql, $this->connection);
			if (mysql_errno($this->connection)) {
				Log::write(mysql_error($this->connection), Log::ERR);
				return false;
			}
		} else {
			Log::write('assert is_resource($this->connection) returs false');
		}
	}
	
	public function dbObject() {
		if (is_resource($this->result)) {
			return mysql_fetch_object($this->result);
		}
	}
	
	public function dbFreeMemory() {
		if (is_resource($this->result)) {
			mysql_free_result($this->result);
		}
	}
	
	public function dbEscapeString($string) {
		return mysql_escape_string($string);
	}

	public function dbLastId() {
		if (is_resource($this->connection)) {
			return mysql_insert_id($this->connection);
		}
	}
	
	public function dbClose() {
		if (is_resource($this->connection)) {
			mysql_close($this->connection);
		}
	}
}